package com.tengju.user.infrastructure.repository;

import com.tengju.user.domain.model.login.UserType;
import com.tengju.user.domain.model.sms.OperationType;
import com.tengju.user.domain.model.sms.SmsVerifyCodeRepository;
import com.tengju.user.infrastructure.persistence.redis.dataobject.*;
import com.tengju.user.infrastructure.persistence.redis.SmsCodeCacheRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @author wuziyang
 */
@Repository
public class SmsVerifyCodeRepositoryImpl implements SmsVerifyCodeRepository {

    @Autowired
    SmsCodeCacheRepository smsCodeCacheRepository;

    @Override
    public Long remainLimitTime(UserType userType,OperationType operationType,String areaCode,String mobile) {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.SMS_LIMIT, "_" + userType.getVal() + "_" + operationType.getVal() + "_"  + areaCode + mobile);
        return smsCodeCacheRepository.remainLimitTime(redisKeyObject);
    }

    @Override
    public boolean checkLimitTime(UserType userType,OperationType operationType,String areaCode,String mobile,long expireTime) {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.SMS_LIMIT, "_" + userType.getVal() + "_" + operationType.getVal() + "_"  + areaCode + mobile);
        return smsCodeCacheRepository.checkLimitTime(redisKeyObject,mobile,expireTime);
    }

    @Override
    public boolean saveSmsVerifyCode(UserType userType, OperationType operationType, String areaCode, String mobile, String code, long expireTime) {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.SMS_CODE, "_" + userType.getVal() + "_" + operationType.getVal() + "_"  + areaCode + mobile);
        return smsCodeCacheRepository.saveSmsCode(redisKeyObject,code,expireTime);
    }

    @Override
    public boolean delSmsVerifyCode(UserType userType, OperationType operationType, String areaCode, String mobile) {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.SMS_CODE, "_" + userType.getVal() + "_" + operationType.getVal() + "_"  + areaCode + mobile);
        return smsCodeCacheRepository.delSmsCode(redisKeyObject);
    }

    @Override
    public String querySmsVerifyCode(UserType userType, OperationType operationType, String areaCode, String mobile) {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.SMS_CODE, "_" + userType.getVal() + "_" + operationType.getVal() + "_"  + areaCode + mobile);
        return smsCodeCacheRepository.querySmsCode(redisKeyObject);
    }

    @Override
    public boolean savePwdChangeToken(UserType userType,String areaCode, String mobile, String value, long expireTime) {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.PWD_CHANGE, "_" + userType.getVal() + "_" + areaCode + mobile);

        return smsCodeCacheRepository.savePwdChangeToken(redisKeyObject, value, expireTime);
    }

    @Override
    public String getPwdChangeToken(UserType userType, String areaCode, String mobile) {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.PWD_CHANGE, "_" + userType.getVal() + "_" + areaCode + mobile);
        return smsCodeCacheRepository.getPwdChangeToken(redisKeyObject);
    }
}
